home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 24 / AACD 24.iso / AACD / Programming / imageio.lib / dev / examples / readprogressive / main.c next >
Encoding:
C/C++ Source or Header  |  2000-08-09  |  4.6 KB  |  187 lines

  1. /* This example use of imageio.library loads the image file specified
  2.         on the command line and views it halved in size in a cybergraphics
  3.         window. It demonstrates the use of a render callback hook.
  4. */
  5.  
  6. #include <stdio.h>
  7.  
  8. #include <dos/dos.h>
  9. #include <exec/memory.h>
  10. #include <exec/types.h>
  11.  
  12. #include <clib/dos_protos.h>
  13. #include <clib/exec_protos.h>
  14. #include <clib/cybergraphics_protos.h>
  15. #include <clib/intuition_protos.h>
  16.  
  17. #include <pragmas/dos_pragmas.h>
  18. #include <pragmas/exec_pragmas.h>
  19. #include <pragmas/intuition_pragmas.h>
  20. #include <pragmas/cybergraphics_pragmas.h>
  21.  
  22. #include <cybergraphics/cybergraphics.h>
  23.  
  24. #include <imageio/imageio.h>
  25. #include <imageio/imageio_protos.h>
  26. #include <imageio/imageio_pragmas.h>
  27.  
  28. /* Function prototypes */
  29. __saveds __asm ULONG progressFunc( register __d0 ULONG curr, register __d1 ULONG lines, register __a0 void *userdata );
  30. __saveds __asm ULONG renderFunc( register __a0 UBYTE *buffer, register __d0 ULONG scanline, register __d1 ULONG rowbytes, register __a1 void *userdata );
  31. ULONG OpenCyberGfx( ULONG x, ULONG y );
  32. void CloseCyberGfx( void );
  33.  
  34. extern struct Library *DOSBase;
  35. struct Library *ImageIOBase, *IntuitionBase;
  36. struct CyberGfxBase *CyberGfxBase;
  37. struct Window *win;
  38. ULONG x, y, bpp;
  39. UBYTE cs;
  40.  
  41. void main( int argc, char **argv )
  42. {
  43.     if ( argv[1] != NULL )
  44.     {
  45.         ImageIOBase = OpenLibrary( "imageio.library", 2 );
  46.         IntuitionBase = OpenLibrary( "intuition.library", NULL );
  47.         if ( IntuitionBase && ImageIOBase )
  48.         {
  49.             struct ImageHandle *ih;
  50.             ULONG err;
  51.             BPTR fp;
  52.  
  53.             fp = Open( argv[1], MODE_OLDFILE );
  54.             if ( fp != NULL )
  55.             {
  56.                 err = AllocImage( &ih,
  57.                     IMG_SrcFile, fp,
  58.                     TAG_DONE );
  59.                 if ( !err )
  60.                 {
  61.                     ULONG num = 1, denom = 2;
  62.                     ULONG rs;
  63.                     UBYTE *buffer, it;
  64.  
  65.                     err = GetImageAttrs( ih,
  66.                         IMG_ImageType, &it,
  67.                         TAG_DONE );
  68.                     if ( !err )
  69.                     {
  70.                         printf("Image type=%d\n",it);
  71.                     }
  72.  
  73.                     err = GetImageAttrs( ih,
  74.                         IMG_Width, &x,
  75.                         IMG_Height,&y,
  76.                         IMG_BytesPerPixel, &bpp,
  77.                         IMG_ColourSpace, &cs,
  78.                         IMG_RowSize, &rs,
  79.                         IMG_TestScaleNum, num,
  80.                         IMG_TestScaleDenom, denom,
  81.                         TAG_DONE );
  82.                     if ( !err )
  83.                     {
  84.                         printf( "width=%ld, height=%ld\n", x, y );
  85.                         printf( "bytes per pixel=%ld, colourspace=%d\n", bpp, cs );
  86.                         printf( "row size=%ld\n", rs );
  87.  
  88.                         if ( OpenCyberGfx( x, y ) )
  89.                         {
  90.                             err = ReadImage( ih,
  91.                                 IMG_ScaleNum, num,
  92.                                 IMG_ScaleDenom, denom,
  93.                                 IMG_ImageBuffer, &buffer,
  94.                                 IMG_ProgressHook, progressFunc,
  95.                                 IMG_RenderHook, renderFunc,
  96.                                 TAG_DONE );
  97.                             if ( !err )
  98.                             {
  99.                                 struct Message *msg;
  100.  
  101.                                 Wait( 1L << win->UserPort->mp_SigBit );
  102.                                 while ( ( msg = GetMsg( win->UserPort ) ) != NULL ) ReplyMsg( msg );
  103.  
  104.                                 /* Display image */
  105.                                 CloseCyberGfx();
  106.                             }
  107.                             else printf( "read image error:%d\n", err );
  108.                         }
  109.                     }
  110.                     else printf( "get image attrs error:%d\n", err );
  111.  
  112.                     FreeImage( ih );
  113.                 }
  114.                 else printf( "alloc image error:%d\n", err );
  115.  
  116.                 Close( fp );
  117.             }
  118.             else printf( "cant open file\n" );
  119.         }
  120.  
  121.         if ( ImageIOBase ) CloseLibrary( ImageIOBase );
  122.         if ( IntuitionBase ) CloseLibrary ( IntuitionBase );
  123.     }
  124.     else printf( "no file specified\n" );
  125. }
  126.  
  127. __saveds __asm ULONG progressFunc( register __d0 ULONG curr, register __d1 ULONG lines, register __a0 void *userdata )
  128. {
  129.     static int prevpercent = 0;
  130.  
  131.     int percent = ( curr * 100 ) / lines;
  132.  
  133.     if ( prevpercent != percent )
  134.     {
  135.         if ( percent % 10 == 0 ) printf( "%d%%\n", percent );
  136.     }
  137.  
  138.     prevpercent = percent;
  139.  
  140.     return NULL;
  141. }
  142.  
  143. __saveds __asm ULONG renderFunc( register __a0 UBYTE *buffer, register __d0 ULONG scanline, register __d1 ULONG rowbytes, register __a1 void *userdata )
  144. {
  145.     WritePixelArray( buffer, 0, 0, rowbytes, win->RPort, 0, scanline, x, 1, RECTFMT_RGB );
  146.  
  147.     return NULL;
  148. }
  149.  
  150. ULONG OpenCyberGfx( ULONG x, ULONG y )
  151. {
  152.     ULONG ret = FALSE;
  153.  
  154.     CyberGfxBase = (struct CyberGfxBase *)OpenLibrary( "cybergraphics.library", 0L );
  155.     if ( CyberGfxBase )
  156.     {
  157.         win = OpenWindowTags( NULL,
  158.             WA_Title, "Proof",
  159.             WA_Flags, WFLG_ACTIVATE | WFLG_SIMPLE_REFRESH |
  160.                 WFLG_SIZEGADGET | WFLG_RMBTRAP | WFLG_DRAGBAR |
  161.                 WFLG_DEPTHGADGET | WFLG_CLOSEGADGET,
  162.             WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW |
  163.                 IDCMP_SIZEVERIFY | IDCMP_NEWSIZE | IDCMP_RAWKEY,
  164.             WA_Left, 16,
  165.             WA_Top, 16,
  166.             WA_Width, x,
  167.             WA_Height, y,
  168.             TAG_DONE );
  169.  
  170.         if ( win != NULL )
  171.         {
  172.             ret = TRUE;
  173.         }
  174.         else printf( "failed to open window\n" );
  175.     }
  176.     else printf( "failed to open cybergfx.library\n" );
  177.  
  178.     return ret;
  179. }
  180.  
  181. void CloseCyberGfx( void )
  182. {
  183.     if ( win ) CloseWindow( win );
  184.  
  185.     if ( CyberGfxBase ) CloseLibrary ( (struct Library *)CyberGfxBase );
  186. }
  187.